from common.util import questionnaireDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

#登录测试
class login:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver = questionnaireDriver.driver
        self.driver.get(self.url)

#正确用例
    #授权用户
    def login_test01(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()

        text = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, "body > div:nth-child(7) > div > div > div > div > div > span:nth-child(2)")))

        questionnaireDriver.screenshot()
        assert text.text == "登录成功！"

    #非授权用户
    def login_test02(self):
        self.driver.back()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("niubi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()

        text = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH, "//div[@class='ant-message']//span[text()='未授权访问']")
            )
        )

        questionnaireDriver.screenshot()
        assert text.text == "未授权访问"

#错误用例
    #账号错误
    def login_test03(self):
        self.driver.back()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("niu")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        text = WebDriverWait(self.driver, 3).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div")))

        questionnaireDriver.screenshot()
        assert text.text == "错误的用户名和密码"

    # 密码错误
    def login_test04(self):

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("bi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button > span").click()
        text = WebDriverWait(self.driver, 3).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR,
             "#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div")))

        questionnaireDriver.screenshot()
        assert text.text == "错误的用户名和密码"

    #账号为空
    def login_test05(self):
        element = self.driver.find_element(By.CSS_SELECTOR, "#username")
        element.send_keys(Keys.CONTROL + "a")  # 全选
        element.send_keys(Keys.DELETE)

        text = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#username_help > div")))

        questionnaireDriver.screenshot()
        assert text.text == "用户名是必填项！"

    #密码为空
    def login_test06(self):
        element = self.driver.find_element(By.CSS_SELECTOR, "#password")
        element.send_keys(Keys.CONTROL + "a")  # 全选
        element.send_keys(Keys.DELETE)

        text = WebDriverWait(self.driver, 5).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#password_help > div")))

        questionnaireDriver.screenshot()
        assert text.text == "密码是必填项！"
